package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.ArticlesListDetailsActivity;
import com.zzk.lanouhn.myapplication1.activity.CollectArticlesDetailsActivity;
import com.zzk.lanouhn.myapplication1.modles.AllViewsList;

/**
 * 消息二级页面中大家都在看实体类
 * Created by lanouhn on 16/9/5.
 */
public class AllViewsAdapter extends RecyclerView.Adapter<AllViewsAdapter.ViewHolder> {
    private Context context;
    private AllViewsList allViewsList;

    public AllViewsAdapter(Context context, AllViewsList allViewsList) {
        this.context = context;
        this.allViewsList = allViewsList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_allviews_list,null);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final AllViewsList.DataBean.TopicBean bean=allViewsList.getData().getTopic().get(position);
        holder.tv_title.setText(bean.getTitle());
        if (!bean.getPic().equals("")) {
            Picasso.with(context).load(bean.getPic()).into(holder.iv_icon);
        } else {
            Picasso.with(context).load(bean.getPics().get(0).getUrl()).into(holder.iv_icon);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.getType().equals("2")&&bean.getType_id().equals("1")) {
                    Intent intent=new Intent(context, ArticlesListDetailsActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("内容",allViewsList.getData().getTopic().get(position).getId());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
                if (bean.getType().equals("")&&bean.getType_id().equals("")) {
                    Intent intent=new Intent(context, CollectArticlesDetailsActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("收藏文章内容",allViewsList.getData().getTopic().get(position).getId());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return allViewsList.getData().getTopic().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_icon;
        private TextView tv_title;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon_allviews_item);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title_allviews_item);
        }
    }
}
